﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.HospitalNavigationCommand;
using MediatR;

namespace D.Medicalcare.Read.API.Application.Handler.HospitalNavigationHandler
{
    /// <summary>
    /// 反填医院导航ID的处理器
    /// </summary>
    public class GetHospitalNavigationIdHandler : IRequestHandler<GetHospitalNavigationIdCommand, APIResult<List<HospitalNavigation>>>
    {
        private readonly IBaseRepository<HospitalNavigation> baseRepository;
        /// <summary>
        /// 构造函数，注入依赖项
        /// </summary>
        /// <param name="baseRepository">存储</param>
        public GetHospitalNavigationIdHandler(IBaseRepository<HospitalNavigation> baseRepository)
        {
            this.baseRepository = baseRepository;
        }
        /// <summary>
        /// 处理反填医院导航ID的逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<List<HospitalNavigation>>> Handle(GetHospitalNavigationIdCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<List<HospitalNavigation>> result = new APIResult<List<HospitalNavigation>>();

                var entity = baseRepository.GetAll().FirstOrDefault(x => x.HospitalNavigationId == request.HospitalNavigationId);
                result.Code = APIEnum.Success;
                result.Msg = "查询成功";

                result.Data = new List<HospitalNavigation>() { entity };
                return await Task.FromResult(result);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
